﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EyFramework.AutoCode.Entity;
using EyFramework.Common.Utils;
using SqlSugar;

namespace EyFramework.AutoCode
{
    /// <summary>
    /// 实现类
    /// </summary>
    public class AutoBuildService
    {
        public bool BuildModel(DbConfigure dbConfigure, string connectNm, string tableNm, string className, string nameSpace, string directory, List<string> exclueFiles)
        {
            //1、建立数据库连接
            SqlSugarClient client = CreateClient(dbConfigure, connectNm);
            client.MappingTables.Clear();
            client.IgnoreColumns.Clear();
            client.MappingTables.Add(className, tableNm);
            exclueFiles.ForEach(p =>
            {
                client.IgnoreColumns.Add(p, className);
            });
            //2、生成代码
            client.DbFirst.Where(tableNm).IsCreateAttribute().CreateClassFile(directory, nameSpace);
            return true;
        }

        public bool BuildDao(DbConfigure dbConfigure, string connectNm, string tableNm, string className, string modelName, string nameSpace, string directory)
        {
            var templatePath = DirectoryUtil.GetCurrentDirectory() + "\\Templates\\DaoTemplate.txt";//Dao模版地址
            Methods.CreateDao(templatePath, directory, tableNm, className, modelName, nameSpace);
            return true;
        }

        public bool BuildIDao(DbConfigure dbConfigure, string connectNm, string tableNm, string className, string modelName, string nameSpace, string directory)
        {
            var templatePath = DirectoryUtil.GetCurrentDirectory() + "\\Templates\\IDaoTemplate.txt";//Dao模版地址
            Methods.CreateIDao(templatePath, directory, tableNm, className, modelName, nameSpace);
            return true;
        }

        public bool BuildService(DbConfigure dbConfigure, string connectNm, string tableNm, string className, string modelName, string nameSpace, string directory)
        {
            var templatePath = DirectoryUtil.GetCurrentDirectory() + "\\Templates\\ServiceTemplate.txt";//Dao模版地址
            Methods.CreateService(templatePath, directory, tableNm, className, modelName, nameSpace);
            return true;
        }

        public bool BuildIService(DbConfigure dbConfigure, string connectNm, string tableNm, string className, string modelName, string nameSpace, string directory)
        {
            var templatePath = DirectoryUtil.GetCurrentDirectory() + "\\Templates\\IServiceTemplate.txt";//Dao模版地址
            Methods.CreateIService(templatePath, directory, tableNm, className, modelName, nameSpace);
            return true;
        }

        public List<DbTableInfo> queryAllTables(DbConfigure dbConfigure, string connectNm)
        {
            //1、建立数据库连接
            SqlSugarClient client = CreateClient(dbConfigure, connectNm);
            return client.DbMaintenance.GetTableInfoList();
        }

        private SqlSugarClient CreateClient(DbConfigure dbConfigure, string connectNm)
        {
            SqlSugarClient client = null;
            OracleDb oracleDb = dbConfigure.OracleDbs.FirstOrDefault(p => p.ConnectName == connectNm);
            if (oracleDb != null)
            {
                client = CreateOracleClient(oracleDb);
            }
            //--其他连接的后面继续
            SqlserverDb sqlserverDb = dbConfigure.SqlserverDbs.FirstOrDefault(P => P.ConnectName == connectNm);
            if (sqlserverDb != null)
            {
                client = CreateSqlServerClient(sqlserverDb);
            }
            return client;
        }

        private SqlSugarClient CreateOracleClient(OracleDb oracleDb)
        {
            #region 创建数据库连接

            string ConnectStr =
                "User Id={0};Password={1};Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={2})(PORT={3})))(CONNECT_DATA=(SERVICE_NAME={4})))";
            ConnectStr = string.Format(ConnectStr, oracleDb.UserName,
                oracleDb.Password,
                oracleDb.HostIp,
                oracleDb.Port,
                oracleDb.ServiceSidName);
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = ConnectStr, //必填, 数据库连接字符串
                DbType = SqlSugar.DbType.Oracle, //必填, 数据库类型
                IsAutoCloseConnection = true //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
            });

            #endregion 创建数据库连接

            return db;
        }

        private SqlSugarClient CreateSqlServerClient(SqlserverDb sqlserverDb)
        {
            #region 创建数据库连接

            string ConnectStr = "Data Source={0};Database={1};User ID={2};Password={3}";
            ConnectStr = string.Format(ConnectStr, sqlserverDb.HostIp,
                sqlserverDb.DataBaseName,
                sqlserverDb.UserName,
                sqlserverDb.Password);
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = ConnectStr, //必填, 数据库连接字符串
                DbType = SqlSugar.DbType.SqlServer, //必填, 数据库类型
                IsAutoCloseConnection = true //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
            });

            #endregion 创建数据库连接

            return db;
        }
    }
}